﻿using TomatoClock.Models;
using TomatoClock.Services;
using TomatoClock.ViewModels;
using MaterialSkin;
using MaterialSkin.Controls;
using System.IO.Packaging;

namespace TomatoClock.Views
{
    public partial class Login : Form
    {
        private MainViewModel viewModel;
        private DataService dataService;
        private readonly MaterialSkinManager materialSkinManager;
        public Login(DataService dataService)
        {
            InitializeComponent();
            this.dataService = dataService;
            /* materialSkinManager = MaterialSkinManager.Instance;
             materialSkinManager.EnforceBackcolorOnAllComponents = true;
             materialSkinManager.AddFormToManage(this);
             materialSkinManager.Theme = MaterialSkinManager.Themes.LIGHT;
             materialSkinManager.ColorScheme = new ColorScheme(
                        Primary.Cyan700,
                        Primary.Cyan900,
                        Primary.Cyan500,
                        Accent.DeepOrange200,
                        TextShade.WHITE);*/
        }

        private void Login_Load(object sender, EventArgs e)
        {

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {

            Start start = new Start(dataService);
            this.Hide();
            start.ShowDialog();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            string username = textBox1.Text;
            string password = textBox3.Text;


            if (dataService.LoginUser(username, password))
            {
                MessageBox.Show("登陆成功！");
                User user = dataService.GetUserFromUserNameAnduserPassword(username, password);
                if (user == null)
                {
                    Console.WriteLine("user not exisit");
                    MessageBox.Show("用户不存在");
                }
                else
                {
                    //MessageBox.Show("连接成功，用户id=" + user.UserId);
                    MainForm mainForm = new MainForm(new MainViewModel(dataService, user));
                    this.Hide();
                    mainForm.ShowDialog();
                }
            }
            else
            {
                MessageBox.Show("账号或密码错误，请重新输入！");
            }
        }
    }
}
